package com.gongpingjialoan.activity.tool;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.fb.MyPushIntentService;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.activity.main.MainActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.data.LicenceRecordData;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.StepRecord;
import com.gongpingjialoan.widget.LoadingDialog;

import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * 车辆违章详情界面           wang
 *
 * @author Administrator
 */
public class LicenceResultActivity extends BaseActivity {

    private LicenceResultActivity mySelf = this;

    private LicenceRecordData recordData;

    private Button backImg;

    private ListView recordListView;

    private RecordAdapter adapter;

    private TextView plateView, countView, fenView, moneyT;

    private String licencePlate, provNameForShort, province, engineNumber, city_pinyin, chejia;

    private LoadingDialog loadingDialog;

    private View view;

    private String fromType;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_licence_result);
        loadingDialog = new LoadingDialog(this);
        initBase(mySelf);
        setTitle("违章详情");
        initView();
        view = findViewById(R.id.main);
        plateView = (TextView) findViewById(R.id.plate_number);
        countView = (TextView) findViewById(R.id.record_count);
        fenView = (TextView) findViewById(R.id.fen_view);
        moneyT = (TextView) findViewById(R.id.money);


    }


    private void initView() {
        Intent intent = getIntent();
        fromType = intent.getStringExtra("type");
        if (MyPushIntentService.VIOLATION.equals(fromType)) {
            String data = intent.getStringExtra("typevalue");
            //推送用
            String source = intent.getStringExtra("source");
            getTuisongData(data);
            StepRecord.recordStep(this, "pushnotification", "{type:" + fromType + ",typevalue:" + data + ",source:" + source + "}");
        } else {
            licencePlate = intent.getStringExtra("license_num");
            provNameForShort = intent.getStringExtra("licence_prefix");
            province = intent.getStringExtra("province");
            city_pinyin = intent.getStringExtra("city_pinyin");
            engineNumber = intent.getStringExtra("engine_number");
            chejia = intent.getStringExtra("body_number");
        }
        initData();
    }


    private void getTuisongData(String data) {
        if (TextUtils.isEmpty(data)) {
            return;
        }
        String[] datas = data.split("/");
        if (datas.length == 6) {
            city_pinyin = datas[0];
            province = datas[1];
            provNameForShort = datas[2];
            licencePlate = datas[3];
            engineNumber = datas[4];
            chejia = datas[5];
        } else if (datas.length == 5) {
            city_pinyin = datas[0];
            province = datas[1];
            provNameForShort = datas[2];
            licencePlate = datas[3];
            engineNumber = datas[4];
        } else if (datas.length == 4) {
            city_pinyin = datas[0];
            province = datas[1];
            provNameForShort = datas[2];
            licencePlate = datas[3];
        }

    }


    @Override
    public void initTitleBar() {
        View backV = findViewById(R.id.title_back);
        if (backV != null) {
            backV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    onBackPressed();
                }
            });
        }
    }


    @Override
    public void onBackPressed() {
        //表示从推送而来
        if (MyPushIntentService.VIOLATION.equals(fromType)) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        } else {
            super.onBackPressed();
        }
    }


    private void initData() {
        loadingDialog.show();
        NetDataJson mNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {

            @Override
            public void onDataJsonError(String errorMessage) {
                loadingDialog.dismiss();
                view.setVisibility(View.GONE);
                showTips(4, errorMessage);
            }

            @Override
            public void onDataJsonUpdate(JSONObject json) {
                // progressDialog.dismiss();
                LicenceRecordData recordData = null;
                loadingDialog.dismiss();
                try {
                    if (json.getInt("count") <= 0) {
                        showTips(3, "恭喜，您没有任何违章记录");
                        view.setVisibility(View.GONE);
                        return;
                    }
                    view.setVisibility(View.VISIBLE);
                    recordData = new LicenceRecordData(licencePlate, json);
                    plateView.setText(recordData.getPlateNumber());
                    countView.setText(String.valueOf(recordData.getCount()) + "次");
                    fenView.setText(String.valueOf(recordData.getFen()) + "分");
                    moneyT.setText(recordData.getTotalmoney() + "元");

                    recordListView = (ListView) findViewById(R.id.list_record);
                    adapter = new RecordAdapter(mySelf, recordData.getRecordList());
                    recordListView.setAdapter(adapter);
                    adapter.notifyDataSetChanged();

                } catch (Exception e) {
                    view.setVisibility(View.GONE);
                    showTips(4, "数据异常，请检查网络连接或稍后重试。");
                }

            }
        });


        mNetDataJson.setUrl(API.query_violation);
        mNetDataJson.addParam("license_num", licencePlate);
        mNetDataJson.addParam("licence_prefix", provNameForShort);
        mNetDataJson.addParam("province", province);

        if (!TextUtils.isEmpty(engineNumber)) {
            mNetDataJson.addParam("engine_number", engineNumber);
        }
        mNetDataJson.addParam("city_pinyin", city_pinyin);
        if (!TextUtils.isEmpty(chejia)) {
            mNetDataJson.addParam("body_number", chejia);
        }

        mNetDataJson.request("get");
    }


    class RecordAdapter extends BaseAdapter {
        private Context context;

        private List<Map<String, String>> records;

        public RecordAdapter(Context context, List<Map<String, String>> recs) {
            this.context = context;
            this.records = recs;
        }

        @Override
        public int getCount() {
            return records.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        public final class ViewHolder {
            TextView actionView;

            TextView dateView;

            TextView areaView;

            TextView fenView;

            TextView moneyView;

            ImageView chuliView;

        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            Map<String, String> record = records.get(position);
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.list_licence_result, null);
                holder = new ViewHolder();
                holder.actionView = (TextView) convertView.findViewById(R.id.action);
                holder.areaView = (TextView) convertView.findViewById(R.id.area);
                holder.dateView = (TextView) convertView.findViewById(R.id.date);
                holder.fenView = (TextView) convertView.findViewById(R.id.fen);
                holder.moneyView = (TextView) convertView.findViewById(R.id.money);
                holder.chuliView = (ImageView) convertView.findViewById(R.id.chuli);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.actionView.setText(record.get("act"));
            holder.areaView.setText(record.get("area"));
            holder.dateView.setText(record.get("date"));
            holder.fenView.setText(record.get("fen"));
            holder.moneyView.setText(record.get("money"));

            if (record.get("chuli").equals("未处理")) {
                holder.chuliView.setImageResource(R.drawable.license_record_status_undealt);
            } else {
                holder.chuliView.setImageResource(R.drawable.license_record_status_dealt);
            }
            return convertView;
        }
    }
}
